/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vale.controllers;

import javax.ejb.EJB;

import javax.inject.Named;
import javax.servlet.ServletException;
import org.vale.dao.UserDAO;
import org.vale.entity.User;
import javax.faces.view.ViewScoped;

/**
 *
 * @author Gabriel Bran gbran.u@gmail.com
 */
@ViewScoped
@Named(value = "loginBean")
public class LoginController extends AbstractController implements java.io.Serializable {

    private String username;
    private String password;
    @EJB
    private UserDAO userDAO;

    public LoginController() {
    }

    public String login() {
        try {
            _login(username, password);
            User user = userDAO.find(username);
            if (!user.getCodstatus().equalsIgnoreCase("A")) {
                addErrorMessage("El usuario: " + username + " se encuentra " + (user.getCodstatus().equalsIgnoreCase("B") ? "bloqueado" : "inactivo") + ",  contacte a su administrador.");
                _logout();
                return null;
            }
            return "home";
        } catch (ServletException e) {
            addErrorMessage("Usuario o password incorrecto.");
            LOGGER.error("Error al iniciar Session", e);
        }

        return null;
    }

    public String logout() {
        _logout();
        return "login";
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
